﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UniCT.LinkShareWpf.Interface;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Channels;

namespace UniCT.LinkShareWpf.CommunicationChannel
{
    public class Channel : IDisposable
    {
        IContract _channel = null;
        ChannelFactory<IContract> _factory = null;

        public IContract Client
        {
            get
            {

                return _channel;
            }

        }

        //public Channel(string target, int port, string serviceName)
        //{

            //UriBuilder ub = new UriBuilder(Uri.UriSchemeHttp, target, port, serviceName);
            //Uri address = ub.Uri;

        public Channel(Uri address)
        {
           
            
            BasicHttpBinding bindingType = new BasicHttpBinding();
            bindingType.OpenTimeout = new TimeSpan(0, 0, 5);
            bindingType.CloseTimeout = new TimeSpan(0, 0, 5);
            bindingType.ReceiveTimeout = new TimeSpan(0, 0, 5);
            bindingType.SendTimeout = new TimeSpan(0, 0, 5);
            
            
            
            EndpointAddress epAddress = new EndpointAddress(address);
            _factory = new ChannelFactory<IContract>(bindingType, epAddress);
            _channel = _factory.CreateChannel();


        }

        public void Dispose()
        {
            try
            {

                _factory.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine("Channels.cs" + e.Message);
            }
        }


    }
}